package com.ruoyi.wetest.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;


public class JsonHelper {

    protected static final Logger log = LoggerFactory.getLogger(JsonHelper.class);

    public static JSONObject jsonFileLoad(ResourceLoader resourceLoader, String fileName) {
        Resource jsonFile = resourceLoader.getResource("classpath:" + fileName);
        String jsonStr;
        try (InputStream inputStream = jsonFile.getInputStream()) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            jsonStr = stringBuilder.toString();

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return JSON.parseObject(jsonStr);

    }

}